//
//  UITextField+LimitLength.m
//  APlan
//
//  Created by he15his on 14-9-15.
//  Copyright (c) 2014年 jingling. All rights reserved.
//

#import "UITextField+Extra.h"
#import <objc/runtime.h>

static const void *UtilityKey = &UtilityKey;

@implementation UITextField (Extra)

@dynamic block;

- (void)setLeftImageName:(NSString *)imageName {
    UIImage *image = [UIImage imageNamed:imageName];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, image.size.width + 30, CGRectGetHeight(self.frame))];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, (CGRectGetHeight(self.frame) - image.size.width) / 2, image.size.width, image.size.height)];
    [imageView setImage:image];
    [view addSubview:imageView];
    self.leftView = view;
    self.leftViewMode = UITextFieldViewModeAlways;
    
    
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, image.size.width + 30, CGRectGetHeight(self.frame))];
    self.rightView = rightView;
    self.rightViewMode = UITextFieldViewModeAlways;
}

- (void)addSecretButton {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 46, CGRectGetHeight(self.frame))];
    
    UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
    sender.frame = CGRectMake(15, (CGRectGetHeight(self.frame) - 15) / 2, 17, 15);
    [sender setImage:[UIImage imageNamed:@"password_normall"] forState:UIControlStateNormal];
    [sender setImage:[UIImage imageNamed:@"password_press"] forState:UIControlStateSelected];
    [sender addTarget:self action:@selector(secretAction:) forControlEvents:UIControlEventTouchUpInside];
    sender.selected = YES;
    [view addSubview:sender];
    
    self.secureTextEntry = YES;

    self.rightView = view;
    self.rightViewMode = UITextFieldViewModeAlways;
}

- (void)addSendButton {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 46, CGRectGetHeight(self.frame))];
    
    UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
    sender.frame = CGRectMake(0, 0, 33, CGRectGetHeight(self.frame));
    sender.titleLabel.font = [UIFont fontWithName:@"PingFang SC" size: 14];
    [sender setTitle:JFMainLocalizedString(@"send") forState:UIControlStateNormal];
    [sender setTitleColor:[UIColor colorWithRed:248/255.0 green:167/255.0 blue:40/255.0 alpha:1.0] forState:UIControlStateNormal];
    [sender addTarget:self action:@selector(secondAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:sender];
    
    self.rightView = view;
    self.rightViewMode = UITextFieldViewModeAlways;
}

- (void)secretAction:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    self.secureTextEntry = sender.isSelected;
}

- (void)secondAction:(UIButton *)sender {
    NSString *string = sender.titleLabel.text;
    if ([string isEqualToString:JFMainLocalizedString(@"send")]) {
        if (self.block){
            self.block();
        }
        [self updateSeconds:sender];
    }
}

- (void)updateSeconds:(UIButton *)sender {
    NSString *string = sender.titleLabel.text;
    if ([string isEqualToString:JFMainLocalizedString(@"send")]) {
        string = @"59S";
    }else {
        string = [string substringToIndex:string.length - 1];
        if (string.intValue == 0) {
            string = JFMainLocalizedString(@"send");
        }else {
            string = [NSString stringWithFormat:@"%dS", string.intValue - 1];
        }
    }
    [sender setTitle:string forState:UIControlStateNormal];
    
    if (![string isEqualToString:JFMainLocalizedString(@"send")]) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self updateSeconds:sender];
        });
    }
}


- (sendBlock)block {
    return objc_getAssociatedObject(self, UtilityKey);
}

- (void)setBlock:(sendBlock)block{
    objc_setAssociatedObject(self, UtilityKey, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


@end
